<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/7 0007
 * Time: 10:44
 */

namespace app\currency\controller;


use library\Controller;
use think\Db;
use think\Exception;

/**
 * 保洁员实名认证
 * Class CleanerAuth
 * @package app\currency\controller
 */
class CleanerAuth extends Controller
{
    protected $table = 'CleanerAuth';

    /**
     * 实名认证列表
     * @menu true
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '申请列表';
        $status = $this->request->get('status');
        if(empty($status)){
            $where = [['status','eq','0']];
        } else {
            $where = [['status','eq',$status]];
        }
        $this->_query($this->table)
            ->where($where)
            ->like('real_name,id_card_number')
            ->dateBetween('create_at')
            ->order('status asc,create_at desc')->page();
    }
    protected function _index_page_filter(&$data)
    {
        $cids = array_unique(array_column($data, 'cid'));
        $cleanerList = Db::name('Cleaner')->whereIn('id', $cids)->select();
        foreach ($data as &$vo) {
            $vo['cleaner'] = [];
            foreach ($cleanerList as $cleaner) if ($cleaner['id'] === $vo['cid']) {
                $vo['cleaner'] = $cleaner;
            }
        }
    }

    /**
     * 驳回申请
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '-1']);
    }

    /**
     * 通过请求
     * @auth true
     */
    public function resume()
    {

        $this->applyCsrfToken();
        $cid = $this->request->post('cid');
        $info = Db::name($this->table)->where('cid','eq',$cid)->find();
        Db::transaction(function() use($cid,$info){
            Db::name('Cleaner')->where('id','eq',$cid)->update([
                'auth_state' => '1',
                'name' => $info['real_name']
            ]);
            Db::name($this->table)->where('cid','eq',$cid)->setField('status','1');
        });
        $this->success('操作成功！');
    }
}